package com.fang.transfer.file.client;

import com.fang.transfer.file.client.connection.TcpUtil;
import com.fang.transfer.file.client.connection.UdpUtil;
import com.fang.transfer.file.client.gui.GUI;
import com.fang.transfer.file.client.transfer.TransferService;
import com.fang.transfer.file.client.transfer.UdpService;
import com.fang.transfer.file.client.util.ClassUtil;
import com.fang.transfer.file.common.RequestListener;
import com.fang.transfer.file.common.model.TransferFileRequest;
import com.fang.transfer.file.common.model.User;

import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class AppContext {
    public static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(1, 1, 5, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(10));


    public static User my;
    public static TcpUtil tcpUtil;
    public static Map<String, UdpService> udpServiceMap = new ConcurrentHashMap<>();
    public final static GUI gui = new GUI();
    public static Map<String, RequestListener> requestListeners = new HashMap<>();
    public static Map<String, TransferFileRequest> transferFileRequestMap = new ConcurrentHashMap<>();
    public static TransferService transferService;
    public static Map<String, RandomAccessFile> randomAccessFileMap = new ConcurrentHashMap<>();

    public final static String charsetName = "utf-8";


    public static void setMy(User my) {
        AppContext.my = my;
    }

    static {
        try {
            Set<Class<?>> requestListenerClasses = ClassUtil.getClasses("com.fang.transfer.file.client.listener");
            for (Class<?> requestListenerClass : requestListenerClasses) {
                Object requestListenerObj = requestListenerClass.newInstance();
                if (!(requestListenerObj instanceof RequestListener)) {
                    continue;
                }
                RequestListener requestListener = (RequestListener) requestListenerObj;
                requestListeners.put(requestListener.getCode(), requestListener);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
